package vn.allmobile.newsreader.customview;

import vn.allmobile.newsreader.utils.Log;

import com.gridlayout.GridLayout;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;

public class ExpandableGridLayout extends GridLayout{
	
	Context _context;
	int _width,_height;
	
	public ExpandableGridLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setColumnCount(2);
		this.setRowCount(3);
		
		_context = context;
	}

	public ExpandableGridLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.setColumnCount(2);
		this.setRowCount(3);
		_context = context;
		
		
	}

	public ExpandableGridLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.setColumnCount(2);
		this.setRowCount(3);
		_context = context;
		
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see android.widget.GridLayout#onMeasure(int, int)
	 */
	@Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
    	/*Log.v("widthMeasureSpec:" +widthMeasureSpec + "heightMeasureSpec: "+ heightMeasureSpec);
    	int heightSpec;

        if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
            // The great Android "hackatlon", the love, the magic.
            // The two leftmost bits in the height measure spec have
            // a special meaning, hence we can't use them to describe height.
            heightSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        }
        else {
            // Any other height should be respected as is.
            heightSpec = heightMeasureSpec;
        }
        */
		setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
        Log.v("widthMeasureSpec:" + _width);
        Log.v("heightSpec:" + _height);
        //super.onMeasure(_width, _height);
    }
	
	
	/**
	 * Determines the width of this view
	 * @param measureSpec A measureSpec packed into an int
	 * @return The width of the view, honoring constraints from measureSpec
	 */
	private int measureWidth(int measureSpec) {
	    int result = 0;
	    int specMode = MeasureSpec.getMode(measureSpec);
	    int specSize = MeasureSpec.getSize(measureSpec);

	    if (specMode == MeasureSpec.EXACTLY) {
	        // We were told how big to be
	        result = specSize;
	    } else {
	        DisplayMetrics dm = new DisplayMetrics(); 
	        ((WindowManager)_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm); 
	        result = dm.widthPixels; 
	    }
	    _width = result;
	    return result;
	}

	/**
	 * Determines the height of this view
	 * @param measureSpec A measureSpec packed into an int
	 * @return The height of the view, honoring constraints from measureSpec
	 */
	private int measureHeight(int measureSpec) {
	    int result = 0;
	    int specMode = MeasureSpec.getMode(measureSpec);
	    int specSize = MeasureSpec.getSize(measureSpec);

	    if (specMode == MeasureSpec.EXACTLY) {
	        // We were told how big to be
	        result = specSize;
	    } else {
	    	DisplayMetrics dm = new DisplayMetrics(); 
	        ((WindowManager)_context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm); 
	        result = dm.heightPixels; 
	    }
	    _height = result;

	    return result;
	}
	
	
	
	/* (non-Javadoc)
	 * @see com.gridlayout.GridLayout#onDraw(android.graphics.Canvas)
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
	}

	/* (non-Javadoc)
	 * @see android.view.View#onAttachedToWindow()
	 */
	@Override
	protected void onAttachedToWindow() {
		// TODO Auto-generated method stub
		
		Log.v("onAttachedToWindow:" + this.getMeasuredHeight());
		super.onAttachedToWindow();
	}

	/* (non-Javadoc)
	 * @see com.gridlayout.GridLayout#onLayout(boolean, int, int, int, int)
	 */
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		Log.v("onLayout:" + this.getMeasuredHeight());
		super.onLayout(changed, left, top, right, bottom);
	}
	
	
	
	
	
	

}
